#include "doctorinfo.h"
#include "messagetype.h"
#include <QFont>
#include <QPalette>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QLineEdit>
#include <QMap>
#include <QByteArray>

DoctorInfo::DoctorInfo(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {

    // 设置窗口标题和大小
    setWindowTitle("查看医生信息");
    setFixedSize(600, 700); // 增大窗口大小以容纳更多信息

    QVBoxLayout *layout = new QVBoxLayout(this);

    // 设置窗口背景颜色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(245, 245, 245)); // 浅灰色背景
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // 添加标题标签
    QLabel *label = new QLabel("查看医生信息", this);
    label->setFont(QFont("微软雅黑", 20, QFont::Bold));
    label->setAlignment(Qt::AlignCenter);
    layout->addWidget(label);

    // 添加医生姓名输入框
    QLabel *doctorNameLabel = new QLabel("医生姓名:", this);
    doctorNameLabel->setFont(QFont("微软雅黑", 16));
    layout->addWidget(doctorNameLabel);

    doctorNameEdit = new QLineEdit(this);
    doctorNameEdit->setPlaceholderText("请输入医生姓名");
    doctorNameEdit->setFont(QFont("微软雅黑", 14));
    doctorNameEdit->setFixedHeight(40);
    layout->addWidget(doctorNameEdit);

    // 添加查询按钮
    QPushButton *queryButton = new QPushButton("查询", this);
    queryButton->setFont(QFont("微软雅黑", 16, QFont::Bold));
    queryButton->setStyleSheet("QPushButton {"
                               "background-color: #4CAF50;"
                               "color: white;"
                               "border-radius: 8px;"
                               "padding: 12px;"
                               "} "
                               "QPushButton:hover {"
                               "background-color: #45A049;"
                               "}");
    queryButton->setFixedHeight(50);
    layout->addWidget(queryButton);

    // 添加医生信息显示框
    doctorInfoDisplay = new QTextEdit(this);
    doctorInfoDisplay->setReadOnly(true);
    doctorInfoDisplay->setFont(QFont("微软雅黑", 14));
    doctorInfoDisplay->setStyleSheet("padding: 10px; border: 1px solid #ccc; border-radius: 5px; background-color: #ffffff;");
    layout->addWidget(doctorInfoDisplay);

    // 添加返回按钮
    QPushButton *backButton = new QPushButton("返回", this);
    backButton->setFont(QFont("微软雅黑", 16, QFont::Bold));
    backButton->setStyleSheet("QPushButton {"
                              "background-color: #f44336;"
                              "color: white;"
                              "border-radius: 8px;"
                              "padding: 12px;"
                              "} "
                              "QPushButton:hover {"
                              "background-color: #e53935;"
                              "}");
    backButton->setFixedHeight(50);

    // 添加按钮布局，使返回按钮居中
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch(); // 在按钮之前和之后添加弹性空间
    buttonLayout->addWidget(backButton);
    buttonLayout->addStretch();

    layout->addLayout(buttonLayout);

    // 设置主布局的边距和间距
    layout->setSpacing(20); // 控件之间的间距
    layout->setContentsMargins(30, 30, 30, 30); // 设置边距

    // 连接信号与槽
    connect(queryButton, &QPushButton::clicked, this, &DoctorInfo::onQueryButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &DoctorInfo::onBackButtonClicked);
}

void DoctorInfo::onQueryButtonClicked() {
    QString doctorName = doctorNameEdit->text();
    if (doctorName.isEmpty()) {
        doctorInfoDisplay->setText("请输入医生的姓名以查询信息");
        return;
    }

    QMap<QString, QString> queryData;
    queryData.insert("type", DOCTORINFO);
    queryData.insert("doctorname", doctorName); // 确保这里的键是正确的
    QByteArray data = mapToByteArrayCustom(queryData);

    // 连接服务器并发送请求
    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1", 12345);
    }
    if (tcpSocket->waitForConnected(5000)) {
        tcpSocket->write(data);
        tcpSocket->flush();
        if (tcpSocket->waitForReadyRead(5000)) { // 等待响应
            QByteArray responseData = tcpSocket->readAll();
            QList<QMap<QString, QString>> doctorRecords = convertByteArrayToQMapArrayCustom(responseData);
            if (!doctorRecords.isEmpty()) {
                QString displayText;
                for (const QMap<QString, QString> &record : doctorRecords) {
                    displayText += "医生姓名: " + record.value("doctorname") + "\n";
                    displayText += "科室: " + record.value("departmentname") + "\n";
                    displayText += "电话: " + record.value("phone") + "\n";
                    displayText += "-----------------------\n";
                }
                doctorInfoDisplay->setText(displayText);
            } else {
                doctorInfoDisplay->setText("未找到相关医生信息");
            }
        } else {
            doctorInfoDisplay->setText("未能接收到服务器响应，请稍后重试");
        }
    } else {
        doctorInfoDisplay->setText("无法连接到服务器，请检查网络连接");
    }
}

void DoctorInfo::onBackButtonClicked() {
    this->close();  // 关闭窗口，返回上一级
}
